<template>
  <div>
    <el-card>
      <template #header>商品添加</template>
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="80px"
        size="mini"
      >
        <el-form-item label="商品名称 " prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="商品分类">
          <el-select v-model="form.category" placeholder="请选择活动区域">
            <el-option
              v-for="item in arr"
              :key="item.cateName"
              :label="item.cateName"
              :value="item.cateName"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品价格">
          <el-input-number
            v-model="form.price"
            :min="1"
            :max="100"
            label="描述文字"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="商品图片">
          <el-upload
            v-model="form.imgUrl"
            class="avatar-uploader"
            action="http://127.0.0.1:5000/goods/goods_img_upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="商品描述" prop="desc">
          <el-input
            type="textarea"
            size="mini"
            v-model="form.goodsDesc"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="mini" @click="submitForm('form')"
            >添加商品</el-button
          >
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { addclss, getimges } from '@/api/Goods.js'
export default {
  data() {
    return {
      arr: [],
      form: {
        name: '',
        category: '',
        price: '',
        imgUrl: '',
        goodsDesc: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入商品名称', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        goodsDesc: [
          { required: true, message: '请输入商品描述', trigger: 'blur' },
          {
            min: 3,
            max: 120,
            message: '长度在 3 到 120 个字符',
            trigger: 'blur'
          }
        ]
      },
      imageUrl: ''
    }
  },
  created() {
    this.getimg()
  },
  methods: {
    async getimg() {
      const { categories } = await getimges()
      this.arr = categories
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw)
      this.form.imgUrl = res.imgUrl
    },
    beforeAvatarUpload(file) {
      const arr = ['image/jpeg', 'image/jpg', 'image/gif']
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!arr.includes(file.type)) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return arr && isLt2M
    },
    submitForm(form) {
      this.$refs[form].validate(async valid => {
        if (valid) {
          const { code, msg } = await addclss(this.form)
          if (code === 0) {
            this.$message.success(msg)
            setTimeout(() => {
              this.$router.push('/Goods/GoodsList')
            }, 2000)
          } else {
            this.$message.error(msg)
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },

    handleChange(value) {
      console.log(value)
    }
  }
}
</script>

<style lang="less" scoped>
.el-form {
  width: 400px;
  .el-input {
    width: 200px;
  }
}
/deep/.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
/deep/.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 128px;
  height: 128px;
  line-height: 128px;
  text-align: center;
}
.avatar {
  width: 128px;
  height: 128px;
  display: block;
}
</style>
